package good_src.Implementation;

import java.util.Observable;
import good_src.Client.SwingFacadeGlobal;
import good_src.Interfaces.Afficheur;
import good_src.Interfaces.Capteur;

/**
 * 
 * @author Tangi Midy & Fabien Le Peih Afficheur Realization
 */
public class ConcreteAfficheur implements Afficheur {

	private Object cpt;
	private int loop;
	private Observable canal;
	private String name;
	private Thread clientThread;
	private boolean Stopped;
	private SwingFacadeGlobal clientSwing = SwingFacadeGlobal.getInstance();

	/**
	 * 
	 * @param canal
	 *            The canal to observe
	 */
	public ConcreteAfficheur(Observable canal) {
		this.cpt = 0;
		this.canal = canal;

	}

	/**
	 * 
	 * @see good_src.Interfaces.ObservateurCapteur#update()
	 */
	@Override
	public void update() {
		this.cpt = ((Canal) canal).getValue();
		System.out.println("Valeur de l'afficheur :" + this.name + "    =    "
				+ cpt + " De version " + ((Canal) canal).getVersion());

		clientSwing.setValues(Integer.parseInt(this.name), cpt, ((Canal) canal)
				.getVersion(), ((Canal) canal).getFrequence());
	}

	/**
	 * 
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	@Override
	public void update(Observable o, Object arg1) {
		this.cpt = ((Capteur<Integer>) o).getValue();

		if (Stopped) {
			((Capteur<Integer>) o).setTick(false);
		}
	}

	/**
	 * 
	 * @return cpt
	 */
	public Object getCpt() {
		return cpt;
	}

	/**
	 * Redefinition of the ToString Method() Used for command line
	 */
	public String toString() {
		return "Compteur de l'afficheur " + name + ": " + cpt;
	}

	/**
	 * 
	 * @return this.name
	 */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 
	 * @see good_src.Interfaces.Afficheur#getBoucle()
	 */
	@Override
	public String getBoucle() {
		return "" + loop;
	}

	/**
	 * 
	 * @see good_src.Interfaces.Afficheur#getCompteur()
	 */
	@Override
	public String getCompteur() {
		return "" + cpt;
	}

	/**
	 * 
	 * @see good_src.Interfaces.Afficheur#stopThread()
	 */
	@Override
	public void stopThread() {
		clientThread.interrupt();
		Stopped = true;
	}

	/**
	 * 
	 * @see good_src.Interfaces.ObservateurCapteur#isUpdatable() Not used here
	 */
	@Override
	public boolean isUpdatable() {
		// TODO Auto-generated method stub
		return false;
	}

}
